// pages/supp_order_details/supp_order_details.js
import { copydata } from "~/utils/util"
import baseteam from '~/utils/teamleader.js'
import basesup from '~/utils/supplier.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderbyid: '',
        steps: [],
        active: 0,
        stepstext:"",
        stepstextnew:"",
        
        refusstart: false,
        refusfalsemsg: [
            "团长未达到门槛",
            "团长未达到门槛1",
            "团长未达到门槛2",
            "请输入"
        ],
        refusfalsechange: '团长未达到门槛',
        refusfalsechangeinput: ""
    },
    onclone() {
        this.setData({
            refusstart: false
        })
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 50);
    },
    newlogistics() {
        wx.navigateTo({
            url: `../suppliersample/suppliersample?orderbyid=${this.data.orderbyid}`,
        })
    },
    navelogistics(){
        wx.navigateTo({
            url: `../viewlogistics/viewlogistics?byid=${this.data.ourdersdetail.contactNumber}&orderbyid=${this.data.orderbyid}`,
        })
    },
    navereturnlogistics(){
        wx.navigateTo({
            url: `../viewlogistics/viewlogistics?byid=${this.data.ourdersdetail.returnNumber}&orderbyid=${this.data.orderbyid}`,
        })
    },
    noreturngroup(){
        wx.navigateTo({
            url: `../returngroup/returngroup?orderbyid=${this.data.orderbyid}`,
        })
    },
    openrefusstart() {
        this.setData({
            refusstart: true
        })
    },
    unpoadimg() {
        let numb = 6
        wx.chooseMessageFile({//调用选择文件接口
            count: numb,//文件数量
            type: 'file',
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFiles
                tempFilePaths.forEach(i => {
                    wx.uploadFile({//调用上传文件接口
                        url: request.API + "/fileUpload/upload-image", //这个接口是后台给的
                        filePath: i.path,
                        name: 'file',
                        method: "post",
                        header: {//headers记住传Authorization也就是token值，不然会返回401未登录
                            "content-type": "multipart/form-data",
                            "Authorization": wx.getStorageSync("token")
                        },

                        success(res) {
                            //上传成功后的一些操作
                            wx.hideLoading();
                            let rs = JSON.parse(res.data);
                            if (rs.code == 0) {
                                var str = rs.data.path
                                var newStr = str.slice(0, 4) + 's' + str.slice(4)
                                const k = `${e.target.dataset.type}[${e.target.dataset.key}].filevalue`;
                                for (var i = 0; i < tempFilePaths.length; i++) {

                                    files.push({ name: tempFilePaths[i].name, path: newStr })
                                }
                                // files.push({name:tempFilePaths[0].name,path:newStr})
                                that.setData({
                                    [k]: files,
                                    files: files,
                                })
                            } else {
                                that.setData({
                                    // isshow: true,
                                    message: "上传失败,稍后重试"
                                });
                                setTimeout(() => {
                                    that.setData({
                                        //isshow: false,
                                        message: ""
                                    });
                                }, 2500);
                            }
                        }
                    })
                })

            }
        })
    },
    utilcopydata(item) {
        copydata(item.currentTarget.dataset.copyjson)
    },
    async ourdersdata() {
        let reqdata = {
            id: this.data.orderbyid
        }
        let resdata = await baseteam.orderbyid(reqdata)
        if (resdata.message == "操作成功") {
            console.log(resdata.data.result)
            this.setData({
                ourdersdetail: resdata.data.result
            })
            if (resdata.data.result.status == 1) {
                this.setData({
                    active: 1,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'question',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"提交申请",
                    stepstextnew:'',
                })
                return false
            }
            if (resdata.data.result.status == 2) {
                this.setData({
                    active: 2,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'circle',
                            activeIcon: 'question',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"等待商家发货",
                    stepstextnew:'',
                })
                return false
            }
            if (resdata.data.result.status == 3) {
                this.setData({
                    active: 3,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'circle',
                            activeIcon: 'question',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"等待团长收货",
                    stepstextnew:'',
                })
                return false
            }
            if (resdata.data.result.status == 4 && resdata.data.result.openingTime != null){
                this.setData({
                    active: 4,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'question',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"审核未通过",
                    stepstextnew:'',
                })
                return false
            }
            if (resdata.data.result.status == 4) {
                this.setData({
                    active: 4,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'question',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"待团长开团",
                    stepstextnew:'',
                })
                return false
            }
            
            if (resdata.data.result.status == 5) {
                this.setData({
                    active: 4,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'question',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"等待供应商审核",
                    stepstextnew:'',
                })
                return false
            }
            if (resdata.data.result.status == 7) {
                this.setData({
                    active: 5,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"申样完成",
                    stepstextnew:'开团成功，商家已确认。',
                })
                return false
            }
            if (resdata.data.result.status == 6 && resdata.data.result.stopStatus == 1) {
                this.setData({
                    active: 0,
                    steps: [],
                    stepstext:"等待商家审核",
                    stepstextnew:'',
                })
            }
            if (resdata.data.result.status == 6 && resdata.data.result.yesTooTime != null ) {
                this.setData({
                    active: 2,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '寄回样品',
                            inactiveIcon: 'checked',
                            activeIcon: 'question',
                        },
                        {
                            text: '中止完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"等待团长寄回样品",
                    stepstextnew:''
                })
            }
            if (resdata.data.result.status == 6 && resdata.data.result.yesTooTime != null && resdata.data.result.stopStatus == 3) {
                this.setData({
                    active: 0,
                    steps: [],
                    stepstext:"团长已寄出样品，待商家确认样品",
                    stepstextnew:''
                })
                return false
            }
            if (resdata.data.result.status == 8 && resdata.data.result.headCancelTime != null ) {
                this.setData({
                    active: 0,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'clear',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"团长取消申样",
                    stepstextnew:'',
                })
                return false
            }
            if (resdata.data.result.status == 8 && resdata.data.result.rejectionTime != null) {
                this.setData({
                    active: 1,
                    steps: [
                        {
                            text: '提交申请',
                            inactiveIcon: 'checked',
                            activeIcon: 'checked',
                        },
                        {
                            text: '商家审核',
                            inactiveIcon: 'checked',
                            activeIcon: 'clear',
                        },
                        {
                            text: '商家发货',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长收货',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '团长开团',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                        {
                            text: '申样完成',
                            inactiveIcon: 'circle',
                            activeIcon: 'checked',
                        },
                    ],
                    stepstext:"商家审核未通过",
                    stepstextnew:resdata.data.result.refuseCause
                })
                return false
            }
            if (resdata.data.result.status == 8 && resdata.data.result.noTooTime != null ) {
                this.setData({
                    active: 0,
                    steps: [],
                    stepstext:"申样关闭",
                    stepstextnew:'商家同意中止开团，无需还样'
                })
                return false
            }
            if (resdata.data.result.status == 8 && resdata.data.result.yesTooTime != null ) {
                this.setData({
                    active: 0,
                    steps: [],
                    stepstext:"还样完成，商家已确认样品",
                    stepstextnew:''
                })
                return false
            }
            
            // ????
            // if (resdata.data.result.status == 5) {
            //     this.setData({
            //         active:4
            //     })
            //     return false
            // }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            orderbyid: options.orderbyid
        })
        this.ourdersdata()
    },

    refusfalsebind(items) {
        this.setData({
            refusfalsechange: items.detail,
            // refusfalsechangeinput:''
        })
    },

    // 供应商拒绝申请
    async refuseapplic() {
        let msg = this.data.refusfalsechange
        if (this.data.refusfalsechange == '请输入' && this.data.refusfalsechangeinput == '') {
            msg = this.data.refusfalsechangeinput
            wx.showToast({
                title: '未输入拒绝原因',
                icon: 'error',
                duration: 2000
            })
            return false
        }
        let reqdata = {
            "id": this.data.ourdersdetail.id,
            "type": 2,
            cause: msg
        }
        let resdata = await basesup.ourderfalse(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '拒绝成功，即将返回上个页面',
                icon: 'success',
                duration: 2000
            })
            this.onclone()
            setTimeout(() => {
                this.naveback()
            }, 1200);
        } else {
            wx.showToast({
                title: '拒绝失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },
    // 同意申请
    async refuseapplicaccess() {
        let reqdata = {
            "id": this.data.ourdersdetail.id,
            "type": 1,
        }
        let resdata = await basesup.ourderfalse(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '通过',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.onclone()
                this.ourdersdata()
            }, 300);
        } else {
            wx.showToast({
                title: '通过失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },

    // 无需还样
    async noReturn(){
        let reqdata = {
            "id": this.data.ourdersdetail.id,
            "type": '1',
        }
        let resdata = await basesup.ourdertermination(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '无需还样',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.onclone()
                this.ourdersdata()
            }, 300);
        } else {
            wx.showToast({
                title: '操作失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },
    goreturn(){
        wx.navigateTo({
          url: `../returndetails/returndetails?orderbyid=${this.data.orderbyid}`,
        })
    },
    // 需要还样
    yesReturn(){
        this.goreturn()
        // let reqdata = {
        //     "id": this.data.ourdersdetail.id,
        //     "type": '2',
        // }
        // let resdata = await basesup.ourdertermination(reqdata)
        // if (resdata.code == 200) {
        //     wx.showToast({
        //         title: '需要还样',
        //         icon: 'success',
        //         duration: 2000
        //     })
        //     setTimeout(() => {
        //         this.onclone()
        //         this.ourdersdata()
        //     }, 300);
        // } else {
        //     wx.showToast({
        //         title: '操作失败，请重试',
        //         icon: 'error',
        //         duration: 2000
        //     })
        // }
    },

    // 确认退件的收货
    async yesReturnyes(){
        let reqdata = {
            "id": this.data.ourdersdetail.id,
        }
        let resdata = await basesup.ourderReceipt(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '还样收货成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.onclone()
                this.ourdersdata()
            }, 300);
        } else {
            wx.showToast({
                title: '还样收货失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },

    // 确认开团
    async yesreturngroup(){
        let reqdata = {
            "id": this.data.ourdersdetail.id,
            "type":"1",
        }
        let resdata = await basesup.ourderKTaudit(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '开团成功',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.onclone()
                this.ourdersdata()
            }, 300);
        } else {
            wx.showToast({
                title: '开团失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.ourdersdata()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})